<template src="./index.html"></template>

<script>
import MedicalRecordMenstrualHistoryService from "../../../../../_services/medicalRecordMenstrualHistory.service";
export default {
  name: "TianDingPatientMeMenstrualHistoryComponent",
  data() {
    return {
      user: {},
      params: {
        MenstrualID: 0, //月经信息编号
        UserID: 0,
        FirstDate: "", //首次来月经时间
        FirstAge: 0, //首次来月经年龄
        IsAmenorrhea: "1", //是否闭经 = ['0', '1']
        AmenorrheaDate: "", //闭经时间
        MinTime: "3", //月经期 最短时间
        MaxTime: "7", //月经期 最长时间
        IsRegular: "0", //月经期是否规律 = ['0', '1']
        Cycle_MinTime: "15", //月经周期 最小
        Cycle_MaxTime: "26", //月经周期 最大
        Cycle_IsRegular: "0", //月经周期是否规律 = ['0', '1']
        LastDate: "" //末次月经日期
      },
      showTime: false,
      MaxTimeList: [],
      MinTimeList: [],
      showCycle: false,
      MaxCycleTimeList: [],
      MinCycleTimeList: []
    };
  },
  mounted() {
    this.user = JSON.parse(this.local$.getItem("tdUser"));
    this.utils$.getNumberLabel(this.MaxTimeList, 1, 20, "天");
    this.utils$.getNumberLabel(this.MinTimeList, 1, 20, "天");
    this.utils$.getNumberLabel(this.MaxCycleTimeList, 1, 60, "天");
    this.utils$.getNumberLabel(this.MinCycleTimeList, 1, 60, "天");

    this.getMenstrualHistoryByUserID();
  },
  methods: {
    getMenstrualHistoryByUserID() {
      this.indicator$.open("正在加载...");
      let params = {};
      params.userID = this.user.UserID;
      MedicalRecordMenstrualHistoryService.getMenstrualHistoryByUserID(
        params
      ).then(response => {
        if (response && response.Data) {
          this.params = response.Data;
          this.params.IsAmenorrhea = this.params.IsAmenorrhea.toString();
          this.params.MinTime = this.params.MinTime.toString();
          this.params.MaxTime = this.params.MaxTime.toString();
          this.params.IsRegular = this.params.IsRegular.toString();
          this.params.Cycle_MinTime = this.params.Cycle_MinTime.toString();
          this.params.Cycle_MaxTime = this.params.Cycle_MaxTime.toString();
          this.params.Cycle_IsRegular = this.params.Cycle_IsRegular.toString();
          this.utils$.dataFormat(this.params);
        }

        let cd = this.utils$.getCurrentDate();
        console.log(this.params);
        if (this.params.MenstrualID > 0) {
          if (this.params.FirstDate !== "") {
            let FirstDateList = this.params.FirstDate.split("-");
            this.params.FirstDate = `${FirstDateList[0]}-${FirstDateList[1]}`;
          } else {
            this.params.FirstDate = `${cd.year}-${cd.month}`;
          }

          if (this.params.LastDate !== "") {
            this.params.LastDate = this.params.LastDate.split("T")[0];
          } else {
            this.params.LastDate = `${cd.year}-${cd.month}-${cd.day}`;
          }

          if (this.params.AmenorrheaDate !== "") {
            let ADateList = this.params.AmenorrheaDate.split("-");
            this.params.AmenorrheaDate = `${ADateList[0]}-${ADateList[1]}`;
          } else {
            this.params.AmenorrheaDate = `${cd.year}-${cd.month}`;
          }
        } else {
          this.params.FirstDate = `${cd.year}-${cd.month}`;
          this.params.LastDate = `${cd.year}-${cd.month}-${cd.day}`;
          this.params.AmenorrheaDate = `${cd.year}-${cd.month}`;
        }
        this.params.UserID = this.user.UserID;
        let firstDate = new Date(this.params.FirstDate);
        let userDate = new Date(this.user.Birthday);
        this.params.FirstAge = firstDate.getFullYear() - userDate.getFullYear();
        this.indicator$.close();
      });
    },
    focusCheckTime(event) {
      document.activeElement.blur();
    },
    getFirstDate(event) {
      let firstDate = new Date(this.params.FirstDate);
      let userDate = new Date(this.user.Birthday);
      this.params.FirstAge = firstDate.getFullYear() - userDate.getFullYear();
    },
    getMaxTime(event) {
      this.MinTimeList = [];
      this.utils$.getNumberLabel(this.MinTimeList, event, 20, "天");
    },
    getMinTime(event) {
      this.showTime = false;
    },
    getMaxCycleTime(event) {
      this.MinCycleTimeList = [];
      this.utils$.getNumberLabel(this.MinCycleTimeList, event, 60, "天");
    },
    getMinCycleTime(event) {
      this.showCycle = false;
    },
    btnPrev() {
      let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
      menu.prev = "prev";
      this.$emit("change", menu);
    },
    btnSubmit(next) {
      this.indicator$.open("正在保存...");

      console.log(this.params);

      let params = {
        MenstrualID: this.params.MenstrualID, //月经信息编号
        UserID: this.user.UserID,
        FirstDate: this.params.FirstDate, //首次来月经时间
        FirstAge: this.params.FirstAge, //首次来月经年龄
        IsAmenorrhea: parseInt(this.params.IsAmenorrhea), //是否闭经 = ['0', '1']
        AmenorrheaDate: this.params.AmenorrheaDate, //闭经时间
        MinTime: parseInt(this.params.MinTime), //月经期 最短时间
        MaxTime: parseInt(this.params.MaxTime), //月经期 最长时间
        IsRegular: parseInt(this.params.IsRegular), //月经期是否规律 = ['0', '1']
        Cycle_MinTime: parseInt(this.params.Cycle_MinTime), //月经周期 最小
        Cycle_MaxTime: parseInt(this.params.Cycle_MaxTime), //月经周期 最大
        Cycle_IsRegular: parseInt(this.params.Cycle_IsRegular), //月经周期是否规律 = ['0', '1']
        LastDate: this.params.LastDate //末次月经日期
      };

      console.log(params);

      
      if (params.MenstrualID > 0) {
        MedicalRecordMenstrualHistoryService.updateMenstrualHistory(
          params
        ).then(response => {
          this.result(response, next);
        });
      } else {
        MedicalRecordMenstrualHistoryService.addMenstrualHistoryModel(
          params
        ).then(response => {
          this.result(response, next);
        });
      }
    },
    result(response, next) {
      if (response && response.Data > 0) {
        this.toast$("保存成功！");
        if (next !== undefined) {
          let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
          menu.next = "next";
          this.$emit("change", menu);
        }
      } else {
        this.toast$("保存失败！");
      }
      this.getMenstrualHistoryByUserID();
    }
  }
};
</script>
